package com.baibei.widget;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.AppCompatTextView;
import android.util.AttributeSet;

import java.util.ArrayList;
import java.util.List;

/**
 * 排序的TextView
 * Created by ChenRui on 2017/8/9 0009 16:33.
 */
public class OrderByTextView extends AppCompatTextView {

    public enum OrderBy {
        /**
         * 正序
         */
        AES,
        /**
         * 倒序
         */
        DESC,
        /**
         * 默认
         */
        DEFAULT
    }

    public interface OnOrderByChangedListener {

        /**
         * 排序发生改变
         *
         * @param value 新的排序
         */
        void onOrderByChanged(OrderByTextView view, OrderBy value);
    }

    // 默认排序
    private OrderBy mOrderBy = OrderBy.DEFAULT;
    private List<OrderBy> mOrderByList = new ArrayList<>();

    // 升序箭头
    protected Drawable mAESDrawable;
    // 倒序箭头
    protected Drawable mDESCDrawable;

    private OnOrderByChangedListener mOnOrderByChangedListener;

    public OrderByTextView(Context context) {
        super(context);
        init();
    }

    public OrderByTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public OrderByTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mAESDrawable = getResources().getDrawable(R.drawable.ic_order_by_arrow);
        mDESCDrawable = getResources().getDrawable(R.drawable.ic_order_by_desc_arrow);
        mDESCDrawable.setLevel(0);
    }

    public void setOnOrderByChangedListener(OnOrderByChangedListener onOrderByChangedListener) {
        mOnOrderByChangedListener = onOrderByChangedListener;
    }


    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        mOrderByList.add(OrderBy.DEFAULT);
        mOrderByList.add(OrderBy.AES);
        mOrderByList.add(OrderBy.DESC);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        mOrderByList.clear();
    }

    /**
     * 获取排序方式
     */
    public OrderBy getOrderBy() {
        return mOrderBy;
    }

    @Override
    public boolean performClick() {
        performNextOrderBy();
        updateOrderByTextStatus();
        return super.performClick();
    }

    /**
     * 获取当前排序所在的索引
     */
    protected int getCurrentPosition() {
        int size = mOrderByList.size();
        for (int i = 0; i < size; i++) {
            if (mOrderByList.get(i) == mOrderBy)
                return i;
        }
        return 0;
    }

    /**
     * 执行下一个排序
     */
    private void performNextOrderBy() {
        int position = getCurrentPosition() + 1;
        mOrderBy = mOrderByList.get(position % mOrderByList.size()); // 切换当前的排序
        if (mOnOrderByChangedListener != null){
            mOnOrderByChangedListener.onOrderByChanged(this, mOrderBy);
        }
    }

    public void setOrderBy(OrderBy orderBy) {
        mOrderBy = orderBy;
        updateOrderByTextStatus();
    }

    /**
     * 更新当前排序文本状态
     */
    private void updateOrderByTextStatus() {
        setSelected(mOrderBy != OrderBy.DEFAULT);
        Drawable drawable = null;
        switch (mOrderBy) {
            case AES:
                drawable = mAESDrawable;
                break;
            case DESC:
                drawable = mDESCDrawable;
                break;
            case DEFAULT:
                drawable = null;
                break;
        }

        setCompoundDrawablesWithIntrinsicBounds(null, null, drawable, null);
    }


}
